package com.steel.yfmall.cloud.assistant.zg.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.steel.yfmall.cloud.assistant.zg.ZgSBService;
import com.steel.yfmall.cloud.assistant.dto.zg.ZgCallWsdlAPIBaseDTO;
import com.steel.yfmall.cloud.assistant.dto.zg.result.ZgResultBean;
import com.steel.yfmall.cloud.assistant.dto.zg.sb.ResponseZgSystemUserAddressInfo;
import com.steel.yfmall.cloud.assistant.dto.zg.sb.ZgSystemUserAddressInfo;
import com.steel.yfmall.cloud.assistant.utils.constants.ZgCallAPIProperties;
import com.steel.yfmall.cloud.assistant.utils.zg.ZgCallServiceUtils;
import com.steel.yfmall.cloud.assistant.utils.zg.ZgMethodBeanUtil;
import com.steel.yfmall.cloud.assistant.utils.zg.ZgParamUtils;
import com.steel.yfmall.cloud.components.utils.base.APIMsgBean;
import com.steel.yfmall.cloud.components.utils.base.ResponseCode;
import com.steel.yfmall.cloud.components.utils.base.SOAStateBean;
import com.steel.yfmall.cloud.components.utils.utils.MobileBaseBeanUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author yuchaohua
 * @version V1.0
 * @Title: ZgSBServiceImpl
 * @Package com.steel.yfmall.cloud.assistant.biz.service.zg.impl
 * @Description: (用一句话描述该文件做什么)
 * @date 2020/12/10 16:42
 */
@Service
@Slf4j
public class ZgSBServiceImpl implements ZgSBService {

    @Resource
    private ZgCallAPIProperties zgCallAPIProperties;

    @Resource
    private ZgCallServiceUtils zgCallServiceUtils;

    /**
     * 收货人信息调整推送中冠系统
     * @return
     */
    @Override
    public APIMsgBean<ResponseZgSystemUserAddressInfo> synchZgSystemUSerAddressInfo(ZgSystemUserAddressInfo apiParaBean) {
        APIMsgBean<ResponseZgSystemUserAddressInfo> returnBean = new APIMsgBean<>();
        ResponseZgSystemUserAddressInfo resultBean = null;
        try {
            Map<String, String> checkParam = new HashMap<>();
            checkParam.put("custNo", "客户编号");
            checkParam.put("recName", "收货人名称");
            checkParam.put("siteNo", "地点代码");
            checkParam.put("areaNo", "终到地区");
            checkParam.put("inputCode", "动作");
            SOAStateBean soaStateBean = ResponseCode.checkApiParameter(checkParam, apiParaBean);
            if (soaStateBean == null || StringUtils.isNotBlank(soaStateBean.getMessage())) {
                returnBean.setSoaStateBean(soaStateBean);
                return returnBean;
            }
            ZgSystemUserAddressInfo resultUserCompanySale = new ZgSystemUserAddressInfo();
            MobileBaseBeanUtils.copyAllFields(apiParaBean,resultUserCompanySale);
            //填充方法信息
            ZgCallWsdlAPIBaseDTO wsdlUtilBean = new ZgCallWsdlAPIBaseDTO();
            //请求地址
            wsdlUtilBean.setUrl(zgCallAPIProperties.getSb_url());
            //方法名
            wsdlUtilBean.setInterfaceName(ZgMethodBeanUtil.SB03);
            //参数名称
            wsdlUtilBean.setParameterType(ZgMethodBeanUtil.SB_SYSTEM);
            APIMsgBean<String> dataBean= zgCallServiceUtils.commonCallZgSoApi(ZgParamUtils.transToUpperObject(JSON.toJSONString(resultUserCompanySale)),wsdlUtilBean);
            if (dataBean == null || StringUtils.isBlank(dataBean.getData())){
                returnBean.setSoaStateBean(ResponseCode.buildEnumErrorSOAStateBean("地址同步推送中冠系统失败"));
                return returnBean;
            }
            //解析参数得到对应参数
            ZgResultBean zgResultBean = JSONObject.parseObject(dataBean.getData(), ZgResultBean.class);
            if (zgResultBean != null){
                if (zgResultBean.getMsg() != null){
                    resultBean = JSONObject.parseObject(JSON.toJSONString(zgResultBean.getMsg()), ResponseZgSystemUserAddressInfo.class);
                }
                // 失败
                if (!zgResultBean.isOk()) {
                    returnBean.setSoaStateBean(ResponseCode.buildEnumErrorSOAStateBean("地址同步推送中冠系统失败"));
                    if (resultBean != null && StringUtils.isNotBlank(resultBean.getErrorMsg())){
                        returnBean.setSoaStateBean(ResponseCode.buildEnumErrorSOAStateBean(resultBean.getErrorMsg()));
                    }
                    return returnBean;
                }
                returnBean.setSoaStateBean(ResponseCode.buildEnumSuccessSOAStateBean());
                returnBean.setData(resultBean);
            }
        } catch (Exception e){
            returnBean.setSoaStateBean(ResponseCode.buildEnumErrorSOAStateBean("收货人信息调整推送中冠系统信息异常"));
            log.error("收货人信息调整推送中冠系统信息异常 : " + JSONObject.toJSONString(apiParaBean),e);
        }
        return returnBean;
    }

}
